package org.linlinjava.litemall.wx.web;


import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallBenefit;
import org.linlinjava.litemall.db.domain.LitemallBenefitDetail;
import org.linlinjava.litemall.db.service.LitemallBenefitService;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxBenefitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 分享服务
 */
@RestController
@RequestMapping("/wx/benefit")
@Validated
public class WxBenefitController {

    @Autowired
    private LitemallBenefitService litemallBenefitService;

    /**
     * 查询个人收益概览
     * @return 个人收益概览
     */
    @GetMapping("general")
    public Object general(@LoginUser Integer userId) {
        BigDecimal sumBenefit = litemallBenefitService.sumBenefit(userId);
        HashMap<String, Object> result = new HashMap<>();
        if (sumBenefit == null) {
            sumBenefit = BigDecimal.ZERO;
        }
        result.put("all", sumBenefit);
        return ResponseUtil.ok(result);
    }

    /**
     * 查询个人收益记录
     * @param page 分页页数
     * @param limit 分页大小
     * @return 分享追踪记录
     */
    @GetMapping("list")
    public Object list(@LoginUser Integer userId,
                        String type,
                        @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallBenefit> lists = litemallBenefitService.query(userId, page, limit, sort, order, type);
        return ResponseUtil.okList(lists);
    }

    /**
     * 记录用户分享进入事件
     * @param body
     * @return
     */
    @PostMapping("in")
    public Object in(@LoginUser Integer userId, @RequestBody String body) {
//        if (userId == null) {
//            return ResponseUtil.ok();
//        }
//        String source = JacksonUtil.parseString(body, "source"); // 来源
//        String goodsId = JacksonUtil.parseString(body, "goodsId"); // 若是商品分享记录商品id
//        wxBenefitService.record(source, userId, ShareEventConstant.EVENT_IN, goodsId);
//        return ResponseUtil.ok();
        return null;
    }
}
